package com.hxzy.yygh.cmn.service.impl;

import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hxzy.yygh.cmn.listener.DictListener;
import com.hxzy.yygh.cmn.mapper.DictMapper;
import com.hxzy.yygh.cmn.service.DictService;
import com.hxzy.yygh.model.cmn.Dict;
import com.hxzy.yygh.vo.cmn.DictEeVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import org.springframework.web.multipart.MultipartFile;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@Service
public class DictServiceImpl extends ServiceImpl<DictMapper, Dict> implements DictService {
    @Autowired
    private DictMapper dictMapper;

    @Override
    public void exportData(HttpServletResponse response) {
        //设置下载信息
        response.setContentType("application/vnd.ms-excel");
        response.setCharacterEncoding("utf-8");
        String fileName = "dict";
        //选择保存路径
        response.setHeader("Content-disposition", "attachment;filename="+ fileName + ".xlsx");

        //拷贝数据到dictEeVoList
        List<Dict> dictList = baseMapper.selectList(null);
        List<DictEeVo> dictEeVoList = new ArrayList<>();
        for (Dict dict : dictList) {
            DictEeVo dictEeVo = new DictEeVo();
            BeanUtils.copyProperties(dict,dictEeVo);
            dictEeVoList.add(dictEeVo);
        }
        //调用easyExcel的write方法
        try {
            EasyExcel.write(response.getOutputStream(), DictEeVo.class).sheet("数据字典").doWrite(dictEeVoList);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @CacheEvict(value = "dict", allEntries=true)
    @Override
    public void importData(MultipartFile file) {
        try {
            //EasyExcel读取
            EasyExcel.read(file.getInputStream(), DictEeVo.class, new DictListener(baseMapper)).sheet().doRead();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


    @Override
    public String getDictName(String dictCode, String value) {
        if(StringUtils.isEmpty(dictCode)){
            LambdaQueryWrapper<Dict> lqw = new LambdaQueryWrapper<>();
            lqw.eq(Dict::getValue, value);
            Dict dict = dictMapper.selectOne(lqw);
            return dict.getName();
        }else {
            LambdaQueryWrapper<Dict> lqw1 = new LambdaQueryWrapper<>();
            lqw1.eq(Dict::getDictCode, dictCode);
            Dict dict1 = dictMapper.selectOne(lqw1);
            Long id = dict1.getId();

            LambdaQueryWrapper<Dict> lqw2 = new LambdaQueryWrapper<>();
            lqw2.eq(Dict::getParentId, id);
            lqw2.eq(Dict::getValue, value);
            Dict dict2 = dictMapper.selectOne(lqw2);
            return dict2.getName();
        }
    }


    @Override
    public List<Dict> getByDictCode(String dictCode) {
        //根据dictCode获取id
        LambdaQueryWrapper<Dict> lqw = new LambdaQueryWrapper<>();
        lqw.eq(Dict::getDictCode,dictCode);
        Dict dict = dictMapper.selectOne(lqw);

        //根据id获取其子节点
        return this.findChildData(dict.getId());
    }

    @Cacheable(value = "dict",keyGenerator = "keyGenerator")
    @Override
    public List<Dict> findChildData(Long id) {
        //条件构造器
        LambdaQueryWrapper<Dict> lqw = new LambdaQueryWrapper<>();
        lqw.eq(Dict::getParentId,id);

        //传入条件进行查询
        List<Dict> dictList = baseMapper.selectList(lqw);

        // 判断集合中的每一个是否有子节点
        for (Dict dict : dictList) {
            Long dictId = dict.getId();
            boolean hasChildren = isHasChildren(dictId);
            dict.setHasChildren(hasChildren);
        }
        //返回结果
        return dictList;
    }

    //判断是否含有子节点
    private boolean isHasChildren(Long id){
        //条件构造器
        LambdaQueryWrapper<Dict> lqw = new LambdaQueryWrapper<>();
        lqw.eq(Dict::getParentId,id);

        //传入条件进行查询
        Integer count = baseMapper.selectCount(lqw);

        //返回结果
        return count>0;

    }
}
